package com.yicong.ants.ui.coin;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.BarUtils;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.yicong.ants.R;
import com.yicong.ants.databinding.UserCoinActivityBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.ad.AdManager;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.utils.AppUtils2;
import com.zx.sdk.util.ConfigHelper;

import java.util.Objects;
import java.util.Random;

/**
 * 门票 贡献值页
 *
 * @author cch
 * @version 2020/7/15
 */
public class UserCoinActivity extends BaseTitleBarActivity<UserCoinActivityBinding> implements View.OnClickListener {
    private TicketAccountFragment ticketFragment;
    private YcAccountFragment ycAccountFragment;
    private int select = 1;

    @Override
    protected int getLayout() {
        return R.layout.user_coin_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleBarVisible(false);
        AppHelper.transStatus(getWindow());
        BarUtils.setStatusBarLightMode(this, false);
        mDataBind.setClick(this);
        loadAd();

        select = getIntent().getIntExtra("select" , 1);

        ticketFragment = new TicketAccountFragment();
        ycAccountFragment = new YcAccountFragment();

        mDataBind.button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (select != 1) {
                    select = 1;
                    selectText();
                }
            }
        });

        mDataBind.button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (select == 1) {
                    select = 2;
                    selectText();
                }
            }
        });
        selectText();

        if (ConfigHelper.getShowInterstitialPages().contains("ticketBalan")) {
            AdManager.showInterstitial(this);
        }
    }

    private void selectText() {
        ViewGroup.LayoutParams layoutParams;
        if (select == 1) {
            layoutParams = mDataBind.button1.getLayoutParams();
            layoutParams.height = dp2px(44);
            mDataBind.button1.setLayoutParams(layoutParams);
            setSelect(mDataBind.button1, true);
            checkFragment(true, "y", ycAccountFragment);

            layoutParams = mDataBind.button2.getLayoutParams();
            layoutParams.height = dp2px(35);
            mDataBind.button2.setLayoutParams(layoutParams);
            setSelect(mDataBind.button2, false);
            checkFragment(false, "m", ticketFragment);

        } else {
            layoutParams = mDataBind.button2.getLayoutParams();
            layoutParams.height = dp2px(44);
            mDataBind.button2.setLayoutParams(layoutParams);
            setSelect(mDataBind.button2, true);
            checkFragment(true, "m", ticketFragment);

            layoutParams = mDataBind.button1.getLayoutParams();
            layoutParams.height = dp2px(35);
            mDataBind.button1.setLayoutParams(layoutParams);
            setSelect(mDataBind.button1, false);
            checkFragment(false, "y", ycAccountFragment);
        }
    }

    private int dp2px(final float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void setSelect(ViewGroup view, boolean b) {
        view.setSelected(b);
        for (int i = 0; i < view.getChildCount(); i++) {
            if (view.getChildAt(i) instanceof TextView) {
                TextView textView = (TextView) view.getChildAt(i);
                if (b) {
                    textView.setTypeface(Typeface.DEFAULT_BOLD);
                } else {
                    textView.setTypeface(Typeface.DEFAULT);
                }
                return;
            }
        }
    }

    private void checkFragment(boolean isShow, String tag, Fragment fm) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (isShow) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment != null) {
                transaction.show(fragment);
            } else {
                transaction.add(R.id.fragment, fm, tag);
            }
            transaction.commit();
        } else {
            if (fragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.hide(fragment);
                transaction.commit();
            }
        }
    }

    private void loadAd() {
        int randomValue = new Random().nextInt(100);
        int ratio = Integer.parseInt(AppHelper.getUmConfig("user_coin_half_ad", "90"));
        Logs.d("randomValue UserCoinActivity " + randomValue);
        if (randomValue <= ratio) {
            AdManager.showBanner(this, mDataBind.adContainer);
        }
    }

    @Override
    protected void onLoadData() {
    }

    protected void onDestroy() {
        super.onDestroy();
        AdManager.destroyBanner(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        } else if (v.getId() == R.id.coin_rule) {
            ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_rule_coin");
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        Configuration config = base.getResources().getConfiguration();
        config.fontScale = 1f;
        config.densityDpi = (int) ((UiHelper.getScreenWidth() / 411 + 0.5) * 160);
        Context context = base.createConfigurationContext(config);
        super.attachBaseContext(Objects.requireNonNull(new AppUtils2().attachBaseContext(context)));
    }
}
